/**
 * 生成百度链接推送文件
 */
 const fs = require('fs');
 const path = require('path');
 const chalk = require('chalk')
 const matter = require('gray-matter'); // FrontMatter解析器 https://github.com/jonschlinkert/gray-matter
 const readFileList = require('./modules/readFileList');
 const urlsRoot = path.join(__dirname, '..', 'urls.txt'); // 百度链接推送文件
 const DOMAIN = process.argv.splice(2)[0]; // 获取命令行传入的参数

 
 main();
 
 /**
  * 主体函数
  */
 function main() {
   fs.writeFileSync(urlsRoot, DOMAIN)
   const files = readFileList(); // 读取所有md文件数据
 
   files.forEach( file => {
     const { data } = matter(fs.readFileSync(file.filePath, 'utf8')); 
 
     if (data.permalink) {
       const link = `\r\n${DOMAIN}${data.permalink}`;
       console.log(link)
       fs.appendFileSync(urlsRoot, link);
     }
   })
 }